package com.cdks.wuxiu.api.util;

import cn.hutool.json.JSONUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.cdks.wuxiu.common.config.AlipayProps;
import com.cdks.wuxiu.common.exception.ApiException;
import com.ijpay.alipay.AliPayApi;
import com.ijpay.alipay.AliPayApiConfig;
import com.ijpay.alipay.AliPayApiConfigKit;

import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;

@Slf4j
public class AlipayUtils {
    /**
     * 应用ID
     */
    private static final String APP_ID = AlipayProps.appId;
    /**
     * 应用私钥
     */
    private static final String PRIVATE_KEY = AlipayProps.privateKey;
    /**
     * 支付宝公钥
     */
    private static final String PUBLIC_KEY = AlipayProps.publicKey;
    /**
     * 应用公钥证书
     */
    private static final String APP_CERT_PATH = AlipayProps.appCertPath;
    /**
     * 支付宝公钥证书
     */
    private static final String ALI_PAY_CERT_PATH = AlipayProps.aliPayCertPath;
    /**
     * 支付宝根证书
     */
    private static final String ALI_PAY_ROOT_CERT_PATH = AlipayProps.aliPayRootCertPath;
    /**
     * 支付宝支付网关
     */
    private static final String SERVER_URL = AlipayProps.serverUrl;
    /**
     * 支付通知域名
     */
    private static final String DOMAIN = AlipayProps.domain;
    /**
     * 异步回调地址
     */
    public static final String NOTIFY_URL = "api/payment/alipay";

    
    static {
        AliPayApiConfig aliPayApiConfig = getApiConfig();
        AliPayApiConfigKit.putApiConfig(aliPayApiConfig);
        log.info("支付宝支付参数初始化成功:" + JSONUtil.toJsonStr(aliPayApiConfig));
    }

    /**
     * 初始化支付宝参数配置
     */
    public static AliPayApiConfig getApiConfig() {
        return AliPayApiConfig.builder()
                .setAppId(APP_ID)
                .setAliPayPublicKey(PUBLIC_KEY)
                .setAppCertPath(APP_CERT_PATH)
                .setAliPayCertPath(ALI_PAY_CERT_PATH)
                .setAliPayRootCertPath(ALI_PAY_ROOT_CERT_PATH)
                .setCharset("UTF-8")
                .setPrivateKey(PRIVATE_KEY)
                .setServiceUrl(SERVER_URL)
                .setSignType("RSA2")
                // 证书模式
                //.buildByCert();
                // 普通公钥方式
                .build();
    }

    /**
     * 生成 APP 支付订单
     *
     * @param orderNo  订单编号
     * @param totalFee 总金额
     * @param subject  订单标题
     * @param type     回调类型
     * @return 签名参数 直接返回给客户端
     */
    public static String appPay(String orderNo, BigDecimal totalFee, String subject) {
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setOutTradeNo(orderNo);
        model.setTotalAmount(totalFee.toString());
        model.setSubject(subject);

        try {
            String result = AliPayApi.appPayToResponse(model, DOMAIN + NOTIFY_URL).getBody();
            log.info("支付宝APP创建支付订单 result:" + result);
            return result;
        } catch (AlipayApiException e) {
            throw new ApiException(-20, e.getErrMsg());
        }
    }
}
